(function($) {
	var start = 0;
	var sourceData = [];
	var limitNum = 20;
	var pullUpElement = null,isPulling=false;
	var messageTime = '',msgType = '',navName = '';
	var usercode = ''
	mui.init({
		gestures: {longtap: true,release: true},
		swipeBack: false, //启用右滑关闭功能
	});
	mui.plusReady(function() {
		plus.navigator.setStatusBarBackground('#24af96');
		plus.navigator.setStatusBarStyle('light');
		var self = plus.webview.currentWebview();
		self.setStyle({
			popGesture: 'none'  // 作用于ios 禁止右滑关闭
		});
		navName = self.navName; //护理新闻 院内网公告
		msgType = self.msgType; //hlxw,ynwgg

		var perInfo = app.getState()
		usercode = perInfo.usercode
		messageTime = getMessageTime(msgType, usercode);
		var titleEl = document.querySelector('.mui-title');
		titleEl.innerText = navName;
		self.setStyle({
			scrollIndicator: 'none'
		});
		mui('#slider').slider().stopped = true; //禁止左右滑动
		var deceleration = mui.os.ios ? 0.003 : 0.0009;
		mui('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
			deceleration: deceleration
		});
		mui.back = function() {
			plus.navigator.setStatusBarBackground('#fff')
			plus.navigator.setStatusBarStyle('dark');
			var _self = plus.webview.currentWebview();
			_self.close("auto");
		}
		mui('#newsUl').on('tap','li',cellTapped) 
		mui.each(document.querySelectorAll('.mui-slider-group .mui-scroll'), function(index,
		pullRefreshEl) {
			mui(pullRefreshEl).pullToRefresh({
				up: {
					callback: function() {
						requestMessageList(this);
						pullUpElement = this;
					},
					auto: true
				}
			});
		});
		requestMessageList()
	});

	/*
	 * 请求数据 护理部发布的消息
	 * 入参userid^locid^pageNo(页数)^limitNum(每页消息数)
	 * [{"title":'',"author":"","publishTime":"","content":""}]
	 */
	function requestMessageList(endRefresh) {
		if (isPulling) {
			if (endRefresh != null) {
				endRefresh.endPullUpToRefresh(0);
			}
			return
		}
		isPulling = true
		var pdict = {'parr': '',start: start,limit: limitNum}
		var paramDict = {
			'method': 'web.INMPlatform:FindInterNetHosNewsList',
			'params': JSON.stringify(pdict),
			'type': 'RecQuery'
		}
		//console.log(JSON.stringify(pdict))
		mui.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result));
			var arrary = result.data;
			if (start == 0) {
				sourceData.splice(0, sourceData.length);
				if (arrary.length != 0) { //保存当前最新的新闻id
					var firstData = arrary[0];
					var messageRw = firstData.publishTime;
					//saveMessageTime(messageRw, msgType, usercode);
				}
			}
			var tempArr = [];
			for (var i = 0; i < arrary.length; i++) {
				var oneData = arrary[i];
				var newSource = arrary[i].RelDepartment;
				if (newSource == navName) {
					sourceData.push(oneData);
					tempArr.push(oneData);
				}
			}
			var ulEl = document.querySelector('.mui-table-view');
			if (endRefresh != null) {
				endRefresh.endPullUpToRefresh(arrary.length < limitNum);
			}
			updateUI(tempArr);
			start += arrary.length;
			isPulling = false
		}, function(error) {
			xhr = null;
			mui.toast(error);
			if (endRefresh != null) {
				endRefresh.endPullUpToRefresh(0);
			}
			isPulling = false
		});
	}

	function updateUI(arrary) {
		var ulEl = document.getElementById('newsUl');
		var oldLis = ulEl.querySelectorAll('.mui-table-view-cell');
		var index = oldLis.length;
		for (var i = 0; i < arrary.length; i++) {
			var oneData = arrary[i];
			var liEl = document.createElement('li');
			liEl.setAttribute("class", "mui-table-view-cell");
			ulEl.appendChild(liEl);
			var timeStr = oneData.publishTime
			timeStr = timeStr.replace('+', ' ').substring(5, timeStr.length - 3);
			var color = ''
			if (messageTime != '' && oneData.publishTime <= messageTime) {
				color = 'style="color:gray"'
			}
			var html = '<div class="mui-navigate-right">'+
				'<div class="msgTitle" '+color+'>'+specialReplace(oneData.title)+'</div>'+
				'<div class="horizontal-between">'+
					'<p class="msgAuthor" >'+oneData.author+'</p>'+
					'<p class="publish_time" >'+timeStr+'</p>'+
				'</div></div>'
			liEl.innerHTML = html
			var idIndex = i + index;
			liEl.id = idIndex+'-news';
		}
	}
	function cellTapped(){
		var liIndex = parseInt(this.id);
		var newsOneData = sourceData[liIndex];
		var titleEl = this.querySelector('.msgTitle');
		titleEl.style.color = 'gray';
		mui.openWindow({
			url: "messageDetail.html",
			id: "messageDetail",
			extras: {
				newsData: newsOneData,
			},
			waiting: {
				autoShow: false, //自动显示等待框，默认为true
			}
		});
	}
	
	function specialReplace(str) {
		var newStr = str.replace(/@and@/g, '&');
		newStr = newStr.replace(/@nbsp@/g, '&nbsp'); //<br>
		newStr = newStr.replace(/@br@/g, '<br>');
		newStr = newStr.replace(/@《@/g, '<');
		newStr = newStr.replace(/@jianhao@/g, '^');
		return newStr;
	}

	/*
	 * 逻辑说明
	 * 先读取上次存储的messageTime,数据请求结束后取第一个数据的publishTime存到本地,用于下次使用
	 * updateUI时  消息发布时间与messageTime对比
	 * 		已读消息 在本页面 msgTitle.style.color = 'gray';
	 * 		未读消息 在tab-message.css 中.msgTitle的color:#323232;
	 * 未读消息点击后 在本页面 msgTitle.style.color = 'gray';
	 * 
	 * messageTime存储结构 uid-groupCode-分类 护理新闻/护理部发布/院内网动态/院内网公告/护理部消息/科室消息
	 * newMsgDic = {'04480':{'hlxw':'','hlbfb':''},'04430':{},...}
	 * messageTime存/取使用说明
	 * 存 saveMessageTime(messageTime,分类名);
	 *	  例如saveMessageTime('2018-10-26+15:14:11','hlxw');
	 * 取 getMessageTime(分类名);
	 * 	  例如getMessageTime('hlxw');
	 * 
	 */
})(mui)